package com.jt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jt.mapper.RoleAndMenuMapper;
import com.jt.mapper.MenuMapper;
import com.jt.pojo.Menu;
import com.jt.pojo.Role_Menu;
import com.jt.service.MenuService;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private MenuMapper routerMapper;
    @Autowired
    private RoleAndMenuMapper roleAndRouterMapper;

    @Override
    public List<Menu> selectRouterByRouterList(List<String> routerIdList) {
        List<Menu> resultRouterList = new ArrayList<>();
        List<Menu> AllRouterList = routerMapper.selectRouterByRouterList(routerIdList);
        if (!Collections.isEmpty(AllRouterList)) {
            AllRouterList.forEach(item -> {
                if (item.getParentId().equals("1")) {
                    List<Menu> oneChildrenList = AllRouterList.stream().filter(a -> a.getParentId().equals(item.getId())).collect(Collectors.toList());
                    item.setChildren(oneChildrenList);
                    getChildrenListRouter(AllRouterList, oneChildrenList);
                    resultRouterList.add(item);
                }
            });
        }
        return resultRouterList;
    }

    public void getChildrenListRouter(List<Menu> AllRouterList, List<Menu> list) {
        list.forEach(item -> {
            List<Menu> childrenList = AllRouterList.stream().filter(a -> a.getParentId().equals(item.getId())).collect(Collectors.toList());
            item.setChildren(childrenList);
            getChildrenListRouter(AllRouterList, childrenList);
        });
    }

    /*查询所有*/
    @Override
    public List<Menu> selectRouterList() {
        /*查询一级*/
        List<Menu> RouterOneList = routerMapper.selectList(new QueryWrapper<Menu>().eq("parentId", 0));
        RouterOneList.forEach(RouterOne -> {
            List<Menu> children = getChildren(RouterOne.getId());
            RouterOne.setChildren(children);
        });
        return RouterOneList;
    }

    public List<Menu> getChildren(String routerId) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parentId", routerId);
        List<Menu> listRouter = routerMapper.selectList(queryWrapper);
        if (!Collections.isEmpty(listRouter)) {
            listRouter.forEach(a -> {
                List<Menu> children = getChildren(a.getId());
                a.setChildren(children);
            });
        }
        return listRouter;
    }

    @Override
    public int distributionRouter(List<String> menuIdList, String RowRoleId) {
        List<Role_Menu> routerList = new ArrayList<>();
        menuIdList.forEach(oneMenu -> {
            Role_Menu roleAndRouter = new Role_Menu();
            roleAndRouter.setId(UUID.randomUUID().toString().substring(6));
            roleAndRouter.setRoleId(RowRoleId);
            roleAndRouter.setMenuId(oneMenu);
            routerList.add(roleAndRouter);
        });
        return roleAndRouterMapper.addList(routerList);
    }
}
